package com.tgy.algorithm.base._最大回文子串;

public class _最大回文子串 {

    public static int getMaxPalindromeSub(String content) {

        if (content == null || content.length() == 0) {
            return 0;
        }

//        int len = content.length();
        String dividerContent = addDivider(content);
        int len = dividerContent.length();
//        System.out.println(dividerContent);
        int rets = 1;
        int leftIndex;
        int rightIndex;
        char[] chars = dividerContent.toCharArray();
        // O(n^2)
        for (int i = 1; i < len; i++) {
            leftIndex = i - 1;
            rightIndex = i + 1;
            while (true) {
                if (leftIndex < 0 || rightIndex >= len || chars[leftIndex] != chars[rightIndex]) {
                    break;
                }
                leftIndex--;
                rightIndex++;
            }
            rets = Math.max(rets,rightIndex - leftIndex - 1);
        }

        return rets / 2;
    }

    public static String addDivider(String content) {

        StringBuilder sb = new StringBuilder();
        sb.append('#');
        char[] chars = content.toCharArray();
        for (char c : chars) {
            sb.append(c);
            sb.append('#');
        }
        return sb.toString();
    }


    public static void main(String[] args) {
//        String content = "ababbac";
        String content = "ababac";
        int maxPalindromeSub = getMaxPalindromeSub(content);
        System.out.println(maxPalindromeSub);
    }
}
